
//var theURL = "https://tmr11s37vm1:6771/cgi-bin/pmxs/pulselet?-S";
//var theURL = "http://localhost/pulselet";

function webClass() {}

webClass.prototype = {
	_req: null,	//the xmlhttprequest object
	_url: "",
	_user: null,
	_passwd: null,
	_evtListener: null,
        _multipart: false,
	_stat: 0,
	_statText: "",

	get stat() { return this._stat; },
	get statText() { return this._statText; },
	get listener() { return this._evtListener; },
	set listener( listener) { this._evtListener = listener; },

	init: function ( url, user, passwd ) {
		var self = this;
		self._req = new XMLHttpRequest();
		self._req.multipart = self._evtListener._multi;
        	var loc = window.location;
        	url = "http://"+loc.host+url
        
		self._req.addEventListener("progress", function( evt) {
			var len = evt.lengthComputable;
			var loaded = evt.loaded;
			var total = evt.total;
			var type = evt.type;
			var target = evt.target;
			var ephase = evt.eventPhase;
			var text = target.responseText;
			self._stat = target.status;
			self._stattext = target.statusText;
			self._evtListener._onProgress( self._stat, text, loaded );
			
		}, false);
		self._req.addEventListener("load", function ( evt) {
			var len = evt.lengthComputable;
			var loaded = evt.loaded;
			var total = evt.total;
			var type = evt.type;
			var target = evt.target;
			var ephase = evt.eventPhase;
			var text = target.responseText;
			self._stat = target.status;
			self._stattext = target.statusText;
			self._evtListener._onLoad( self._stat, text, loaded );
		}, false);
		self._req.addEventListener("loadstart", function ( evt) {
			var len = evt.lengthComputable;
			var loaded = evt.loaded;
			var total = evt.total;
			var type = evt.type;
			var target = evt.target;
			var ephase = evt.eventPhase;
			var text = target.responseText;
			self._stat = target.status;
			self._stattext = target.statusText;
			var rs = target.readyState; 
			self._evtListener._onLoadStart( self._stat );
		}, false);
		self._req.addEventListener("error", function(evt) {
			var len = evt.lengthComputable;
			var loaded = evt.loaded;
			var total = evt.total;
			var type = evt.type;
			var target = evt.target;
			var ephase = evt.eventPhase;
			var text = target.responseText;
			self._stat = target.status;
			self._stattext = target.statusText;
			var rs = target.readyState; 
			self._evtListener._onError( self._stattext, text );
		}, false);
		self._req.addEventListener("abort", function( evt) {
			var len = evt.lengthComputable;
			var loaded = evt.loaded;
			var total = evt.total;
			var type = evt.type;
			var target = evt.target;
			var ephase = evt.eventPhase;
			var text = target.responseText;
			self._stat = target.status;
			self._stattext = target.statusText;
			var rs = target.readyState; 
			self._evtListener._onAbort( self._stat );
		}, false);
        self._req.withCredentials = false;
		self._req.open("POST", url, true);

	},

	abort: function ( ) {
		this._req.abort();
	},

	send: function (text ) {
		var self = this;
		try {
		self._req.send(text);
		} catch (e) {
			dump(e);
			alert(e);
		}
	},
    setHeader: function( key, val) {
		this._req.setRequestHeader(key,val);
	},
	multi: function() {
		this.__multipart = true;
	}
}

function update_display() {
	server_display();
}

function webListener() {
	this._request = new Object();
}

webListener.prototype = {
    _request: null,
    _url: null,
    _user: null,
    _pass: null,
	_multi: false,

    set request(req) {this._request = req;},
    get request() { return this._request;},
    set url(url) {this._url = url;},
    get url() { return this._url;},
    set user(user) {this._user = user;},
    get user() { return this._user;},
    set passwd(pass) {this._pass = pass;},
    get passwd() { return this._pass;},
    set multipart( ml) {this._multi = ml;},
    get multipart() { return this._multi;},

	_onLoadStart: function ( stat) { 
		dump(this._name+" LOADSTART "+stat+"\n");
	},
	_onProgress: function ( stat, text, len) {
		dump(this._name+" PROGRESS "+text+"\n");
	},
	_onError: function ( stat, text) {
		dump(this._name+" ERROR "+stat+text+"\n");
	},
	_onAbort: function ( stat, state) {
		dump(this._name+" ABORT "+stat+"\n");
	},
	_onLoad: function ( stat, text, len) {
		dump(this._name+" LOAD "+text+"\n");
	},
    send: function(text) {
        try {
            this._request.send(text);
        } catch( e ) {
            dump(e);
            alert(e);
        }
    },
    abort: function() {
        this._request.abort()
    },
	_doit: function (data) {
		var self = this;
		var req = new webClass();
		self._request = req;
		req.listener = self;
		//if (this._multi)
		//	req.multi();
		req.init(self._url, self._user, self._pass);
		try {
			req.send( data);
		} catch (e ) {
			dump(e);
			alert(e);
		}
	},
};
/*
		var rv = parsetext(text);
		dump("servercount "+servers.length+"\n")
		dump("vhosts "+vhosts.length+"\n")
		dump("alerts "+alerts.length+"\n")
		update_display();
*/
